namespace :category do

  desc "新增栏目和旧栏目调整"
  task :update_categories => :environment do
    Category.transaction do
      zonghe_id = Category.where(:cataname => '综合新闻').first.id
      xuesheng = Category.create(:cataname => '学生',:father => zonghe_id)
      shiting =Category.create(:cataname => '视听',:father => zonghe_id)

      zhaoshengjiuye = Category.create(:cataname => '招生就业',:father => 0)
      Category.create(:cataname => '招生',:father => zhaoshengjiuye.id)

      hezuojiaoliu = Category.where(:cataname => '合作交流').first
      Category.create(:cataname => '学术交流',:father => hezuojiaoliu.id)

      Category.where(:cataname => '媒体看华理').first.update_attributes(:cataname => '媒体华理')

      tushuohuali = Category.create(:cataname => '图说华理',:father => 0)

      cate = Category.where(:cataname => '党建专题').first
      cate.update_attributes(:cataname => '党建',:father => zonghe_id)

      cate = Category.where(:cataname => '学院经纬').first
      cate.update_attributes(:cataname => '学院',:father => zonghe_id)

      cate = Category.where(:cataname => '学团在线').first
      ids = cate.children.collect(&:id)
      news = News.where(:category_id => ids)
      news.each do |news|
        news.update_attributes(:category_id => xuesheng.id)
      end
      cate.children.delete_all
      cate.destroy

      cate = Category.where(:cataname => '工会之窗').first
      cate.update_attributes(:cataname => '工会',:father => zonghe_id)

      cate = Category.where(:cataname => '民主党派').first
      cate.update_attributes(:cataname => '党派',:father => zonghe_id)

      cate = Category.where(:cataname => '电视新闻').first
      ids = cate.children.collect(&:id)
      news = News.where(:category_id => ids)
      news.each do |news|
        news.update_attributes(:category_id => shiting.id)
      end
      cate.children.delete_all
      cate.destroy

      cate = Category.where(:cataname => '就业').first
      cate.update_attributes(:father => zhaoshengjiuye.id)

      shendubaodao = Category.where(:cataname => '深度报道').first
      cate = Category.where(:cataname => '华理人物').first
      ids = cate.children.collect(&:id)
      news = News.where(:category_id => ids)
      news.each do |news|
        news.update_attributes(:category_id => shendubaodao.id)
      end
      cate.children.delete_all
      cate.destroy

      news = News.joins(:sptitles).where("sptitles.name" => '校园写真')
      news.each do |news|
        news.update_attributes(:category_id => tushuohuali.id)
      end

    end

  end

end